"use strict";
/* Controllers */
app.controller("rtMetricSyncConfigController", ['$scope', '$compile' , '$location', '$rootScope', '$routeParams', 'rtMetricSyncConfigService', function ($scope, $compile, $location, $rootScope, $routeParams, rtMetricSyncConfigService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listRtMetricSyncConfig = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        rtMetricSyncConfigService.listRtMetricSyncConfig(params,procFn, errorFn);
    };


    $scope.confirmTip = function(type, id, index, param){
        vm.optType = type;
        vm.optId=id;
        vm.optIndex=index;
        vm.optParam=param;
        $('#myModal').modal('show')
    };

    $scope.ok = function(){
        var vm = $scope.vm;
        console.log("optType: "+vm.optType +"," + "optId: "+vm.optId +"," + "optParam: "+vm.optParam);
        if(vm.optType==1) {
            $scope.delItem(vm.optId);
        }else if(vm.optType==9) {
            $scope.enableItem(vm.optId, vm.optIndex, vm.optParam);
        }

        $('#myModal').modal('hide')
    };
    $scope.delItem = function(id) {
        var params = {id: id};
        var procFn = function (response) {
            if (response.success && response.data) {
                $('#list_table').bootstrapTable('remove', {
                    field:'id',
                    values:[vm.optId]
                });
                alert("删除成功！")
            } else {
                alert(response.msg);
            }
        };
        rtMetricSyncConfigService.delRtMetricSyncConfig(params, procFn, errorFn);
    };

    $scope.enableItem = function(id, index, param) {
        var params = {id: id, enable: param};
        var procFn = function (response) {
            if (response.success && response.data) {
                $('#list_table').bootstrapTable('updateRow', {
                    index: index,
                    replace:true,
                    row: {id:id, isEnable:param}
                });
                alert("更新成功！")
            } else {
                alert(response.msg);
            }
        };
        rtMetricSyncConfigService.enableRtMetricSyncConfig(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listRtMetricSyncConfig();
    };


    $scope.initializeController();

}]);

function rtMetricConfigOptFormat(value, row, index) {
    var optCont='<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmTip(1,'+ row.id +','+index+') >删除</a>\n';
    var optCont2='<a type="button" class="btn btn-xs btn-primary" href="#/star/metric/rt_metric_sync_config/'+row.id+'" target="_blank">修改</a>\n';
    var optName = (row.isEnable==1)?'停用':'启用';
    var optVal = (row.isEnable==1)? 0:1;
    var optCont3='<a type="button" class="btn btn-xs btn-primary" onclick=angular.element(this).scope().confirmTip(9,'+ row.id +','+index+','+optVal+') >'+optName+'</a>\n';
    return [
        optCont,
        optCont2,
        optCont3
    ].join('');
}
function enableFormatter(value, row, index) {
    return value==1?'启用':'停用';
}


/* Services */
app.service('rtMetricSyncConfigService', ['ajaxService', function (ajaxService) {

    this.listRtMetricSyncConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/rt_metric_config/list.do", successFunction, errorFunction);
    };

    this.enableRtMetricSyncConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/rt_metric_config/enable.do", successFunction, errorFunction);
    };

    this.delRtMetricSyncConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/rt_metric_config/delete.do", successFunction, errorFunction);
    };

}]);
